package pers.zrryker.mywechatstickers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyWeChatStickers {
    private static String PATH = "D:/emoji";

    private static String[] nameSuffix = {".gif", ".png", ".jpg"};

    public static void main(String[] args) {
        for (int i = 0; i < nameSuffix.length; i++) {
            StringBuffer sb1 = new StringBuffer();
            String path = sb1.append(PATH).append(String.valueOf(i)).toString();
            copyFiles(PATH, path);
        }

        for (int i = 0; i < nameSuffix.length; i++) {
            StringBuffer sb1 = new StringBuffer();
            String path = sb1.append(PATH).append(String.valueOf(i)).toString();
            reNameSuffixByFolder(path, nameSuffix[i]);
        }
        //reName("d:", "111.txt", "222.log");
    }

    /**
     * 拷贝文件
     * @param file
     * @param path
     */
    private static void copyFiles(String oldPath, String newPath) {
        File file = new File(oldPath);
        for (File f1 : file.listFiles()) {
            if (f1.isDirectory()) {
                copyFolder(f1.getAbsolutePath(),
                        getNewfileAbsolutePath(f1.getAbsolutePath(), newPath));
            }
            else if (f1.isFile()) {
                copyFile(f1.getAbsolutePath(),
                        getNewfileAbsolutePath(f1.getAbsolutePath(), newPath));
            }
        }
    }

    /**
     * 修改文件路径
     * @param oldPath 文件原路经v
     * @param pathTop 文件修改路径
     * @return
     */
    private static String getNewfileAbsolutePath(String oldPath, String pathTop) {
        String str = oldPath.substring(oldPath.indexOf("emoji") + 5);
        StringBuffer sb = new StringBuffer();
        String newPath = sb.append(pathTop).append(str).toString();
        System.out.println("newPath:" + newPath);
        return newPath;
    }

    /**
     * 复制单个文件
     * 
     * @param oldPath String 原文件路径 如：c:/fqf.txt
     * @param newPath String 复制后路径 如：f:/fqf.txt
     * @return boolean
     */
    public static void copyFile(String oldPath, String newPath) {
        try {
            FileInputStream fileInputStream = new FileInputStream(oldPath);
            FileOutputStream fileOutputStream = new FileOutputStream(newPath);
            byte[] by = new byte[1024 * 5];
            int len;
            while ((len = fileInputStream.read(by)) != -1) {
                fileOutputStream.write(by, 0, len);
            }
            fileInputStream.close();
            fileOutputStream.close();
        }
        catch (FileNotFoundException e) {
            System.out.println("文件不存在");
            e.printStackTrace();
        }
        catch (IOException e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
        }
    }

    /**
     * 复制整个文件夹内容
     * 
     * @param oldPath String 原文件路径 如：c:/fqf
     * @param newPath String 复制后路径 如：f:/fqf/ff
     * @return boolean
     */
    public static void copyFolder(String oldPath, String newPath) {
        try {
            (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
            File a = new File(oldPath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file[i]);
                }
                else {
                    temp = new File(oldPath + File.separator + file[i]);
                }

                if (temp.isDirectory()) {// 如果是子文件夹
                    copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                }
                else if (temp.isFile()) {
                    String newFilePath = newPath + "/" + (temp.getName()).toString();
                    copyFile(oldPath + File.separator + file[i], newFilePath);
                }
            }
        }
        catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();
        }
    }

    /**
     * 修改文件夹下文件名
     * @param file
     * @param nameSuffix
     */
    public static void reNameSuffixByFolder(String oldPath, String nameSuffix) {
        File file = new File(oldPath);
        for (File f1 : file.listFiles()) {
            if (f1.isDirectory()) {
                System.out.println("AbsolutePath:" + f1.getAbsolutePath());
                reNameSuffixByFolder(f1.getAbsolutePath(), nameSuffix);
            }
            else if (f1.isFile()) {
                String absolutePath = f1.getAbsolutePath();
                String path = absolutePath.substring(0, absolutePath.indexOf(f1.getName()) - 1);
                System.out.println("path:" + path);
                String oldName = f1.getName();
                String newName = f1.getName() + nameSuffix;
                reName(path, oldName, newName);
            }
        }
    }

    /**
     * 修改单个文件名称
     * @param path 文件路径
     * @param oldname 旧文件名
     * @param newname 新文件名
     */
    public static void reName(String path, String oldname, String newname) {
        if (!oldname.equals(newname)) {//新的文件名和以前文件名不同时,才有必要进行重命名 
            File oldfile = new File(path + "/" + oldname);
            File newfile = new File(path + "/" + newname);
            if (!oldfile.exists()) {
                System.out.println("原文件不存在");
                return;//重命名文件不存在
            }
            if (newfile.exists()) {
                //若在该目录下已经有一个文件和新文件名相同，则不允许重命名 
                System.out.println(newname + "已经存在！");
            }
            else {
                oldfile.renameTo(newfile);
                System.out.println("修改完成:oldfile: " + oldfile.getName() + "------>"
                        + newfile.getName());
            }
        }
        else {
            System.out.println("新文件名和旧文件名相同...");
        }
    }
}
